package com.zsc.wxm.ysh.anjo.service.base.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 描述： 日志切面
 *
 * @author playmaker
 * @since 2021/2/10 0:58
 */
@Aspect
@Component
@Slf4j
public class ParamsInterceptor {

    @Around("execution(public * com.zsc.wxm.ysh.anjo.*.controller..*.*(..))")
    public Object controllerLog(ProceedingJoinPoint pdj) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //获取参数数组
        Object[] args = pdj.getArgs();
        //开始时间
        long startTime = System.currentTimeMillis();
        // 记录下请求内容
        StringBuilder sb = new StringBuilder();
        sb.append("\n###请求URL: ").append(request.getRequestURL().toString());
        sb.append("\n   ###IP: ").append(request.getRemoteAddr());
        sb.append("\n   ###请求参数: ").append(Arrays.toString(args));
        sb.append("\n   ###请求方法: ").append(pdj.getSignature().getDeclaringTypeName()).append(".").append(pdj.getSignature().getName());
        //执行原方法并获取返回结果
        Object ret = pdj.proceed(args);
        //结束时间
        long endTime = System.currentTimeMillis();
        sb.append("\n   ###耗时: ").append(endTime - startTime).append("毫秒");
        log.info(sb.toString());
        return ret;

    }
}
